package com.ayoldi.podcastusbsync;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBException;

import org.apache.commons.daemon.Daemon;
import org.apache.commons.daemon.DaemonContext;
import org.apache.commons.daemon.DaemonInitException;
import org.apache.log4j.Logger;

import com.ayoldi.podcastusbsync.configurator.ConfigurationManager;
import com.ayoldi.podcastusbsync.configurator.ConfigurationUtils;
import com.ayoldi.podcastusbsync.downloader.FeedsDownloaderManager;
import com.ayoldi.podcastusbsync.downloader.FeedsDownloaderThread;
import com.ayoldi.podcastusbsync.model.Configuration;
import com.ayoldi.podcastusbsync.model.Configuration.UsbDeviceConfiguration;
import com.ayoldi.podcastusbsync.synchronizer.USBDeviceCheckerThread;
import com.ayoldi.podcastusbsync.synchronizer.USBDeviceSynchronizer;
import com.ayoldi.podcastusbsync.synchronizer.USBDeviceSynchronizerManager;

public class PodcastUSBSync implements Daemon{

	static Logger LOG = Logger.getLogger(PodcastUSBSync.class);
	
	
	private static PodcastUSBSync podcastUSBSyncInstance = new PodcastUSBSync();	
	
	private static USBDeviceSynchronizerManager usbSync;
	private static FeedsDownloaderManager feedsDownloaderManager;
	
	public static void main (String args[]){
		
		File file = new File("configuration.xml");

		ConfigurationManager configManager = new ConfigurationManager(file);
		
		usbSync=new USBDeviceSynchronizerManager(configManager);
		usbSync.startDevicesSynchronizer();
		
		feedsDownloaderManager=new FeedsDownloaderManager(configManager);		
		feedsDownloaderManager.startFeedsDownloader();
		
		
		LOG.info("PodcastUSBSync initialized succesfully!");		
		
		
	}

	/**
     * Static methods called by prunsrv to start/stop
     * the Windows service.  Pass the argument "start"
     * to start the service, and pass "stop" to
     * stop the service.
     *
     * Taken lock, stock and barrel from Christopher Pierce's blog at http://blog.platinumsolutions.com/node/234
     *
     * @param args Arguments from prunsrv command line
     **/
    public static void windowsService(String args[]) {
        String cmd = "start";
        if (args.length > 0) {
            cmd = args[0];
        }

        if ("start".equals(cmd)) {
        	podcastUSBSyncInstance.windowsStart();
        }
        else {
        	podcastUSBSyncInstance.windowsStop();
        }
    }

    public void windowsStart() {
    	LOG.debug("windowsStart called");
    	initialize();
    }

    public void windowsStop() {
    	LOG.debug("windowsStop called");
        terminate();
        synchronized(this) {
            // stop the start loop
            this.notify();
        }
    }

    /**
     * Do the work of starting the engine
     */
    private void initialize() {
    	main(null);
    }
    
    /**
     * Cleanly stop the engine.
     */
    public void terminate() {
    	usbSync.stop();
    	feedsDownloaderManager.stop();
    }
    
    // Implementing the Daemon interface is not required for Windows but is for Linux
    public void init(DaemonContext arg0) throws Exception {
    	LOG.debug("Daemon init");
    }

    public void start() {
    	LOG.debug("Daemon start");
    	initialize();
    }

    public void stop() {
    	LOG.debug("Daemon stop");
        terminate();
    }

    public void destroy() {
    	LOG.debug("Daemon destroy");
    }

    
	
}
